layui.use(["form", "layer", "laypage"], function () {
    var $ = layui.jquery, form = layui.form, layer = layui.layer, laypage = layui.laypage;
    var count1 = $("input[name='totalPages1']").val();
    var count2 = $("input[name='totalPages2']").val();
    var iframes = parent.layer.getFrameIndex(window.name);
    var urls=window.location.pathname;
    if(urls.indexOf("/user/oauth")!=-1){
       var u= parent.document.getElementById("layui-custom-us");
       $(u).html($("input[name='userName']").val());
    }else {
        var u= parent.document.getElementById("layui-custom-bm");
        $(u).html($("input[name='userName']").val());
    }
    laypage.render({
        elem: 'pager1'
        , count: parseInt(count1)
        , curr: parseInt($("input[name='page1']").val())
        , limit: 6
        , theme: '#1E9FFF'
        , groups: 2
        , jump: function (obj, first) {
            $("input[name='page1']").val(obj.curr);
            if (!first) {
                $(".layui-form").eq(0).submit();
            }
        }
    });
    laypage.render({
        elem: 'pager2'
        , count: parseInt(count2)
        , curr: parseInt($("input[name='page2']").val())
        , limit: 6
        , theme: '#1E9FFF'
        , groups: 2
        , jump: function (obj, first) {
            console.log(obj.curr);
            $("input[name='page2']").val(obj.curr);
            if (!first) {
                $(".layui-form").eq(0).submit();
            }
        }
    });
    var value1 = $("input[name='checked1']").val();
    var value2 = $("input[name='checked2']").val();
    var bool1 = false;
    var bool2 = false;
    $(".choose").each(function () {
        if (value1.indexOf($(this).val() + ",") != -1) {
            $(this).prop("checked", true);
            bool1 = true;
        }
    });
    $(".choose1").each(function () {
        if (value2.indexOf($(this).val() + ",") != -1) {
            $(this).prop("checked", true);
            bool2 = true;
        }
    });
    if (bool1 == true) {
        $(".allChoose").prop("checked", true);
    }
    if (bool2 == true) {
        $(".allChoose1").prop("checked", true);
    }
    form.render("checkbox");
    form.on('checkbox(allChoose)', function (data) {
        $(".choose").prop("checked", data.elem.checked);
        var checked = $("input[name='checked1']");
        addOrRemoveAll(".choose", data.elem.checked, checked);
        form.render("checkbox");
    });

    function addOrRemoveAll(data, type, checked) {
        var checkedVal = checked.val();
        if (type == true) {//全选
            $(data).each(function () {
                var val = $(this).val();
                if (checkedVal.indexOf(val + ",") == -1) {
                    checkedVal = checkedVal + val + ",";
                }
            });
        } else {
            $(data).each(function () {
                var val = $(this).val();
                if (checkedVal.indexOf(val + ",") != -1) {
                    checkedVal = checkedVal.replace(val + ",", "");
                }
            })
        }
        checked.val(checkedVal);
    }

    /**通过单选按钮控制全选**/
    form.on("checkbox(choose)", function (data) {
        var check = false;
        var value = data.value;
        if (data.elem.checked) {//如果选中
            check = true;
            var checkedValue = $("input[name='checked1']").val();
            if (checkedValue.indexOf(value + ",") == -1) {
                $("input[name='checked1']").val(checkedValue + value + ",");
            }
        } else {
            var checkedValue = $("input[name='checked1']").val();
            if (checkedValue.indexOf(value + ",") != -1) {
                $("input[name='checked1']").val(checkedValue.replace(value + ",", ""));
            }
            $(".choose").each(function (i) {
                if ($(this).prop("checked") == true) {
                    check = true;
                }
            });
        }
        $(".allChoose").prop("checked", check);
        form.render("checkbox");
    });
    form.on("radio(info)",function (data) {
        $(".layui-form").eq(0).submit();
    });
    form.on("radio(info1)",function (data) {
        $(".layui-form").eq(0).submit();
    });
    form.on('checkbox(allChoose1)', function (data) {
        $(".choose1").prop("checked", data.elem.checked);
        var checked = $("input[name='checked2']");
        addOrRemoveAll(".choose1", data.elem.checked, checked);
        form.render("checkbox");
    })
    /**通过单选按钮控制全选**/
    form.on("checkbox(choose1)", function (data) {
        var check = false;
        var value = data.value;
        if (data.elem.checked) {//如果选中
            check = true;
            var checkedValue = $("input[name='checked2']").val();
            if (checkedValue.indexOf(value + ",") == -1) {
                $("input[name='checked2']").val(checkedValue + value + ",");
            }
        } else {
            var checkedValue = $("input[name='checked2']").val();
            if (checkedValue.indexOf(value + ",") != -1) {
                $("input[name='checked2']").val(checkedValue.replace(value + ",", ""));
            }
            $(".choose1").each(function (i) {
                if ($(this).prop("checked") == true) {
                    check = true;
                }
            });
        }
        $(".allChoose1").prop("checked", check);
        form.render("checkbox");
    });
    /**
     * 分配功能
     */
    $(".custom-role-add").on("click", function () {
        var checked = $("input[name='checked1']").val();
        var ids = $("input[name='idsTech']").val();
        if ("" == ids) {
            if ("" != checked) {
                checked=checked.substring(0, checked.length - 1)
                $("input[name='idsTech']").val(checked);
            }
        } else {
            checked=checked + ids;
            $("input[name='idsTech']").val(checked);
        }

        $("input[name='checked1']").val("");
        $("input[name='checked2']").val("");
        var scheduleRoles=parent.document.getElementById("scheduleRoles");
        $(scheduleRoles).val(checked);
        $(".layui-form").eq(0).submit();
    });
    $(".custom-user-add").on("click", function () {
        var checked = $("input[name='checked1']").val();
        var ids = $("input[name='idsTech']").val();
        if ("" == ids) {
            if ("" != checked) {
                checked=checked.substring(0, checked.length - 1)
                $("input[name='idsTech']").val(checked);
            }
        } else {
            checked=checked + ids;
            $("input[name='idsTech']").val(checked);
        }

        $("input[name='checked1']").val("");
        $("input[name='checked2']").val("");
        var scheduleRoles=parent.document.getElementById("scheduleUsers");
        $(scheduleRoles).val(checked);
        $(".layui-form").eq(0).submit();
    });
    $(".custom-role-remove").on("click", function () {
        var checked = $("input[name='checked2']").val();
        var idsTech = $("input[name='idsTech']").val() + ",";
        if ("" != checked) {
            checked=checked.substring(0,checked.length-1);
            var check = checked.split(",");
            console.log(idsTech);
            for (var i = 0; i < check.length; i++) {
                if (idsTech.indexOf(check[i] + ",") != -1) {
                    console.log(check[i]);
                    idsTech = idsTech.replace(check[i] + ",", "");
                    console.log(idsTech);
                }
            }
        }
        if (idsTech != "") {
            idsTech = idsTech.substring(0, idsTech.length - 1);
        }
        $("input[name='idsTech']").val(idsTech);
        $("input[name='checked1']").val("");
        $("input[name='checked2']").val("");
        var scheduleRoles=parent.document.getElementById("scheduleRoles");
        $(scheduleRoles).val(idsTech);
        $(".layui-form").eq(0).submit();
    });
    $(".custom-user-remove").on("click", function () {
        var checked = $("input[name='checked2']").val();
        var idsTech = $("input[name='idsTech']").val() + ",";
        if ("" != checked) {
            checked=checked.substring(0,checked.length-1);
            var check = checked.split(",");
            console.log(idsTech);
            for (var i = 0; i < check.length; i++) {
                if (idsTech.indexOf(check[i] + ",") != -1) {
                    console.log(check[i]);
                    idsTech = idsTech.replace(check[i] + ",", "");
                    console.log(idsTech);
                }
            }
        }
        if (idsTech != "") {
            idsTech = idsTech.substring(0, idsTech.length - 1);
        }
        $("input[name='idsTech']").val(idsTech);
        $("input[name='checked1']").val("");
        $("input[name='checked2']").val("");
        var scheduleRoles=parent.document.getElementById("scheduleUsers");
        $(scheduleRoles).val(idsTech);
        $(".layui-form").eq(0).submit();
    });
    $(".coutom-span").on("click",function () {
        $(".layui-form").eq(0).submit();
    });
});